﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace lingopen
{
    /// <summary>
    /// 本地日志记录
    /// </summary>
    public class LocalLog
    {
        int _hashCode = 0;
        public LocalLog()
        {
            _hashCode = 0;
        }
        /// <summary>
        /// 构造一个hashcode参数的本地日志对象
        /// </summary>
        /// <param name="hashCode"></param>
        public LocalLog(int hashCode)
        {
            _hashCode = hashCode;
        }
        private static string writeLock = "";
        /// <summary>
        /// 记录本地日志文件
        /// </summary>
        /// <param name="content">内容</param>
        public void Log(string content)
        {
            lock (writeLock)
            {
                var time = DateTime.Now;
                var dirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Logs", time.ToString("yyyyMM"));
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
                //半小时一个日志文件
                string filePath = Path.Combine(dirPath, time.ToString("yyyyMMddHH") + ".log");
                using (StreamWriter sw = new StreamWriter(filePath, true, Encoding.UTF8))
                {
                    var pid = _hashCode;// System.Diagnostics.Process.GetCurrentProcess().Id;
                    Console.WriteLine(time.ToString("yyyy-MM-dd HH:mm:ss") + " " + pid + " " + content);
                    sw.WriteLine(time.ToString("yyyy-MM-dd HH:mm:ss") + " " + pid + " " + content);
                }
            }
        }
        /// <summary>
        /// 记录本地日志文件
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="objs">可变参数</param>
        public void Log(string content, params object[] objs)
        {
            lock (writeLock)
            {
                var time = DateTime.Now;
                var dirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Logs", time.ToString("yyyyMM"));
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
                string log = string.Format(content, objs);
                //半小时一个日志文件
                string filePath = Path.Combine(dirPath, time.ToString("yyyyMMddHH") + ".log");
                using (StreamWriter sw = new StreamWriter(filePath, true, Encoding.UTF8))
                {
                    var pid = _hashCode;// System.Diagnostics.Process.GetCurrentProcess().Id;
                    Console.WriteLine(time.ToString("yyyy-MM-dd HH:mm:ss") + " " + pid + " " + log);
                    sw.WriteLine(time.ToString("yyyy-MM-dd HH:mm:ss") + " " + pid + " " + log);
                }
            }
        }
        public void LogTxt(string dir,string content)
        {
            lock (writeLock)
            {
                var time = DateTime.Now;
                var dirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dir, time.ToString("yyyyMM"));
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
                
                //半小时一个日志文件
                string filePath = Path.Combine(dirPath, time.ToString("yyyyMMddHH") + ".log");
                using (StreamWriter sw = new StreamWriter(filePath, true, Encoding.UTF8))
                {
                    var pid = _hashCode;// System.Diagnostics.Process.GetCurrentProcess().Id;
                    Console.WriteLine(time.ToString("yyyy-MM-dd HH:mm:ss.fff ") + content);
                    sw.WriteLine(time.ToString("yyyy-MM-dd HH:mm:ss.fff ") + content);
                }
            }
        }
    }
}
